/**
 * @method
 * @description 获取用户信息
 * @param {String} title 标题
 */
export const getUserProfile = (title = '用于完善会员资料') => {
  return new Promise((resolve, reject) => {
    wx.getUserProfile({
      desc: title, // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        resolve(res)
      },
      fail: (err) => {
        reject(err)
      }
    })
  })
}


/**
 * @method
 * @description 登录获取code码
 * @param {Number} timeout 超时时间
 */
export const getCode = (timeout = 5000) => {
  return new Promise((resolve, reject) => {
    wx.login({
      timeout,
      success(res) {
        resolve(res)
      },
      fail(err) {
        reject(err)
      }
    })
  })
}


/**
 * @method
 * @description 获取用户的当前设置
 */
export const getSetting = () => {
  return new Promise((resolve, reject) => {
    wx.getSetting({
      success(res) {
        resolve(res)
      },
      fail(err) {
        reject(err)
      }
    })
  })
}


/**
 * @method
 * @description 获取用户收货地址
 */
export const chooseAddress = () => {
  return new Promise((resolve, reject) => {
    wx.chooseAddress({
      success(res) {
        resolve(res)
      },
      fail(err) {
        reject(err)
      }
    })
  })
}


/**
 * @method
 * @description 选择图片或视频
 */
export const chooseMedia = () => {
  return new Promise((resolve, reject) => {
    wx.chooseMedia({
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      count: 9,
      success: (res) => {
        resolve(res)
      },
      fail: (err) => {
        reject(err)
      }
    })
  })
}

/**
 * @method
 * @description 将本地资源上传到服务器
 */
export const uploadFile = (img) => {
  return new Promise((resolve, reject) => {
    wx.uploadFile({
      url: 'https://www.liulongbin.top:8888/api/private/v1/upload', //仅为示例，非真实的接口地址
      filePath: img,
      name: 'file',
      header: {
        Authorization: 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo'
      },
      success(res) {
        resolve(res)
      },
      fail(err) {
        reject(err)
      }
    })
  })
}